package com.zhuss.core.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.resource.UpdateResourceBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.ResourceDao;
import com.zhuss.core.db.entity.Resource;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.AabException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 修改菜单资源 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("resourceUpdate")
public class ResourceUpdate extends AbstractProcessor<Object, UpdateResourceBo> implements Processor<Object, UpdateResourceBo> {

    @Autowired
    private ResourceDao resourceDao;
    
    
    @Autowired
	@Qualifier("resourceRead")
	private ResourceRead resourceRead;
    
    
    /**
     * 校验
     */
    @Override
    public void check(UpdateResourceBo bo) throws AabException {
    	Long resourceId = bo.getResourceId();
    	String name = bo.getName();
    	String url = bo.getUrl();
    	Long parent = bo.getParent();
    	if (resourceId == null || resourceId <= 0l) {
    		throw new AabException(ExceptionCode.资源ID为空);
    	}
    	if (name == null || "".equals(name.trim())) {
    		throw new AabException(ExceptionCode.资源name为空);
    	}
    	if (url == null || "".equals(url.trim())) {
    		throw new AabException(ExceptionCode.资源url为空);
    	}
    	if (parent == null || parent < 0l) {
    		throw new AabException(ExceptionCode.资源父级为空);
    	}
    	
    	if (parent.longValue() == resourceId.longValue()) {
    		throw new AabException(ExceptionCode.资源父级不能为自己);
    	}
    	
    	List<Resource> list = resourceRead.listNotId(name, resourceId, bo.getSystem());
    	if (!list.isEmpty()) {
    		// throw new AabException(ExceptionCode.资源name已存在);
    	}
    	
    	List<Resource> urlList = resourceRead.listNotIdByUrl(url, resourceId, bo.getSystem());
    	if (!urlList.isEmpty()) {
    		// throw new AabException(ExceptionCode.资源url已存在);
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(UpdateResourceBo param) {
    	Resource record = new Resource();
    	record.setResourceId(param.getResourceId());
    	record.setName(param.getName());
    	record.setParent(param.getParent());
    	record.setPic(param.getPic());
    	record.setType(param.getType());
    	record.setUrl(param.getUrl());
    	record.setIsPublic(param.getIsPublic());
    	record.setRequestMethod(param.getRequestMethod());
    	record.setSort(param.getSort());
    	record.setSystem(param.getSystem());
    	record.setImg(param.getImg());
    	record.setDisImg(param.getDisImg());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	resourceDao.updateByPrimaryKeySelective(record);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(UpdateResourceBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.菜单资源修改.key());
    	record.setTargetId(param.getResourceId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
